import {Api} from '@/api/type'
import ApiCommon from '@/api/common'

type R<T> = Api.Common.R<T>;
type PageRecord<T> = Api.Common.PageRecord<T>;
type PageParameter<T> = Api.Common.PageParameter<T>;

export class BaseApi<T> {

    private readonly url: string

    constructor(url: string) {
        this.url = url
    }

    /**
     * 获取数据分页
     */

    async getPageInfo(data: PageParameter<T>): Promise<R<PageRecord<T>>> {
        return await ApiCommon.request.get<R<PageRecord<T>>>(`system/${this.url}/page`, data)
    }

    /**
     * 获取数据信息
     */
    async getDataInfo(data: Partial<T> = {}): Promise<R<T>> {
        return await ApiCommon.request.get<R<T>>(`system/${this.url}/info`, data)
    }

    /**
     * 获取数据列表
     */
    async getDataList(data: Partial<T> = {}): Promise<R<Array<T>>> {
        return await ApiCommon.request.get<R<Array<T>>>(`system/${this.url}/list`, data)
    }

    /**
     * 修改数据信息
     */
    async modifyData(data: Partial<T> = {}): Promise<R<boolean>> {
        return await ApiCommon.request.put<R<boolean>>(`system/${this.url}/modify`, data)
    }

    /**
     * 添加数据信息
     */
    async additionData(data: Partial<T> = {}): Promise<R<boolean>> {
        return await ApiCommon.request.post<R<boolean>>(`system/${this.url}/add`, data)
    }

    /**
     * 删除数据信息
     */
    async removeData(identity: string): Promise<R<boolean>> {
        return await ApiCommon.request.delete<R<boolean>>(`system/${this.url}/remove`, {identity: identity})
    }

    /**
     * 批量删除数据信息
     */
    async batchRemoveData(identity: Array<string>): Promise<R<boolean>> {
        return await ApiCommon.request.post<R<boolean>>(`system/${this.url}/batch-remove`, identity)
    }
}

export default BaseApi
